package networking;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
//import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import java.net.Socket;

import android.util.Log;

// TCP client class for communication with server
public class TCPclient implements Runnable {
	  private static final String serverIP = "10.250.65.190"; // ex: 192.168.0.100
	  //private static final String serverIP = "192.168.43.18"; // ex: 192.168.0.100
	  //private static final String serverIP = "10.250.64.167"; // ex: 192.168.0.100
	  private static final int serverPort = 6789; // ex: 5555
	  private String msg;
	  private String return_msg;
	  
	  public TCPclient(String _msg){
	   this.msg = _msg;
	  }
	  @Override
	  public void run() {
	   // TODO Auto-generated method stub
	   try {
	      
	      //InetAddress serverAddr = InetAddress.getByName(serverIP);
	      
	      Log.d("TCP", "C: Connecting...");
	      
	      //Socket socket = new Socket(serverAddr, serverPort);
	      
	      Socket socket = new Socket(serverIP, serverPort);
	      
	     try {
		      Log.d("TCP", "C: Sending: '" + msg + "'");
		      PrintWriter out = 
		    		  new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);

		      out.println(msg);
		      
		      Log.d("TCP", "C: Sent.");
		      Log.d("TCP", "C: Done.");
		         
		      BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		      return_msg = in.readLine();
		         
		      Log.d("TCP", "C: Server send to me this message -->" + return_msg);
	      } catch(Exception e) {
	    	  Log.e("TCP", "C: Error1", e);
	      } finally {
	    	  socket.close();
	      }
	      } catch (Exception e) {
	    	  Log.e("TCP", "C: Error2", e);
	      }
	  }
	  
	  public boolean isMember() {
		  
		  if(return_msg.equals("FALSE") || return_msg.equals("false")) {
			  return false;
		  } else {
			  return true;
		  }
		  
	  }
	  
}